#质心计算过程
import numpy as np
import matplotlib.pyplot as plt
# 样本集
X = np.array([[1, 2], [2, 2], [6, 8], [7, 8]])
#定义初始化质心
C = np.array([[1.0, 2.0], [2.0, 2.0]])
#重复计算质心5次
iters = 5
while (iters>0) :
    iters -= 1
    B = [] #每个样本点到质心的距离
    for c in C:
        #计算每个点到质心的欧式距离
        dis = np.sqrt(((X - c)**2).sum(axis=1))
        # print(dis)
        B.append(dis)
        # print()
    #求样本点属于哪一个类别
    # print(B)
    min_idx = np.argmin(np.array(B),axis=0)
    # print(min_idx)
    for i in range(len(C)):
        #更换每个质心的位置
        C[i] = np.mean(X[min_idx == i],axis=0)
#打印所有样本的所属的簇
print(min_idx)
print(C)

